<?php
namespace app\daohang\loglic;

class Datas
{
    //批量写入插件初始配置
    public function insertConfig()
    {
        return \daicuo\Op::write([
            'theme'             => 'default',
            'theme_wap'         => 'default',
            'slug_first'        => 1,
            'jump_page'         => 1,
            'request_max'       => 0,
            'search_interval'   => 0,
            'search_hot'        => '呆错导航系统,呆错后台管理框架,呆错文章系统,daicuo,feifeicms',
            'type_option'       => '',
            'score_fast_web'    => 10,
            'score_fast_mp'     => 10,
            'score_fast_mini'   => 10,
            'page_size'         => 30,
            'page_max'          => 100,
            'limit_sitemap'     => 100,
            'limit_index_web'   => 60,
            'limit_index_mp'    => 40,
            'limit_index_mini'  => 40,
            'limit_index_category' => 30,
            'limit_index_tag'   => 60,
            'limit_index_hot'   => 10,
            'rewrite_index'     => 'dh$',
            'rewrite_category'  => 'dhtype/:slug/[:pageNumber]',
            'rewrite_tag'       => 'dhtag/:slug/[:pageNumber]',
            'rewrite_search'    => 'dhsearch/[:searchText]/[:pageNumber]',
            'rewrite_filter'    => 'dhfilter/<controll>-<termId>-<pageSize>-<pageNumber>-<sortName>-<sortOrder>',
            'rewrite_detail'    => 'dhinfo/:slug$',
            'index_title'       => '呆错导航系统－[siteName]',
            'index_keywords'    => 'PHP导航系统,免费导航系统,开源导航系统',
            'index_description' => '呆错导航系统是一款免费开源的分类导航建站系统，安全、便捷、高效、是您的建站首选。',
            'sitemap_title'       => '网站地图页－[siteName]',
            'sitemap_keywords'    => 'sitemap,网站地图',
            'sitemap_description' => '[siteName]网站地图页展示网站结构。',
        ], 'daohang', 'config', 'system', '0', 'yes');
    }
    
    //批量添加路由伪静态
    public function insertRoute()
    {
        config('common.validate_name', false);
        
        config('common.validate_scene', false);
        
        config('common.where_slug_unique', false);
        
        return \daicuo\Route::save_all([
            [
                'rule'        => 'dh$',
                'address'     => 'daohang/index/index',
                'method'      => '*',
                'op_module'   => 'daohang',
                'op_controll' => 'route',
                'op_action'   => 'system',
            ],
            [
                'rule'        => 'dhtype/:slug/[:pageNumber]',
                'address'     => 'daohang/category/index',
                'method'      => '*',
                'op_module'   => 'daohang',
                'op_controll' => 'route',
                'op_action'   => 'system',
            ],
            [
                'rule'        => 'dhtag/:slug/[:pageNumber]',
                'address'     => 'daohang/tag/index',
                'method'      => '*',
                'op_module'   => 'daohang',
                'op_controll' => 'route',
                'op_action'   => 'system',
            ],
            [
                'rule'        => 'dhsearch/[:searchText]/[:pageNumber]',
                'address'     => 'daohang/search/index',
                'method'      => '*',
                'op_module'   => 'daohang',
                'op_controll' => 'route',
                'op_action'   => 'system',
            ],
            [
                'rule'        => 'dhfilter/<controll>-<termId>-<pageSize>-<pageNumber>-<sortName>-<sortOrder>',
                'address'     => 'daohang/filter/index',
                'method'      => '*',
                'op_module'   => 'daohang',
                'op_controll' => 'route',
                'op_action'   => 'system',
            ],
            [
                'rule'        => 'dhinfo/:slug$',
                'address'     => 'daohang/detail/index',
                'method'      => '*',
                'op_module'   => 'daohang',
                'op_controll' => 'route',
                'op_action'   => 'system',
            ],
        ]);
    }
    
    //批量添加权限
    public function insertAuth()
    {
        config('common.validate_name', false);
        
        config('common.validate_scene', false);
        
        config('common.where_slug_unique', false);
        
        //权限节点
        $caps = [
            'daohang/search/index',
            'daohang/search/web',
            'daohang/search/mp',
            'daohang/search/mini',
            'daohang/publish/websave',
            'daohang/publish/mpsave',
            'daohang/publish/minisave',
        ];
        
        //默认数据
        $default = [
            'op_name'       => 'vip',
            'op_module'     => 'daohang',
            'op_controll'   => 'auth',
            'op_action'     => 'system',
            'op_order'      => 0,
            'op_status'     => 'normal',
        ];
        
        //批量添加数据
        $dataList = [];
        foreach($caps as $key=>$value){
            array_push($dataList, DcArrayArgs(['op_value'=>$value],$default));
        }
        return \daicuo\Op::save_all($dataList);
    }
    
    //批量添加导航
    public function insertNavs()
    {
        config('common.validate_name', false);
        
        config('common.validate_scene', false);
        
        config('common.where_slug_unique', false);
        
        return \daicuo\Term::save_all([
            //导航栏
            [
                'term_order'      => 9,
                'term_parent'     => 0,
                'term_type'       => 'navs',
                'term_action'     => 'navbar',
                'term_controll'   => 'index',
                'term_module'     => 'daohang',
                'term_status'     => 'normal',
                'term_name'       => '导航首页',
                'term_slug'       => 'daohang',
                'term_info'       => '呆错导航系统首页',
                'navs_url'        => 'daohang/index/index',
                'navs_active'     => 'daohangindexindex',
                'navs_target'     => '_self',
            ],
            [
                'term_order'      => 1,
                'term_parent'     => 0,
                'term_type'       => 'navs',
                'term_action'     => 'navbar',
                'term_controll'   => 'publish',
                'term_module'     => 'daohang',
                'term_status'     => 'normal',
                'term_name'       => '免费发布',
                'term_slug'       => 'dhfabu',
                'term_info'       => '免费发布网站、公众号、小程序',
                'navs_url'        => 'daohang/publish/index',
                'navs_active'     => 'daohangpublishindex',
                'navs_target'     => '_self',
            ],
            [
                'term_order'      => 1,
                'term_parent'     => 0,
                'term_type'       => 'navs',
                'term_action'     => 'navbar',
                'term_controll'   => 'publish',
                'term_module'     => 'daohang',
                'term_status'     => 'normal',
                'term_name'       => '快审服务',
                'term_slug'       => 'ksfu',
                'term_info'       => '免审核发布网站、公众号、小程序',
                'navs_url'        => 'daohang/publish/fast',
                'navs_active'     => 'daohangpublishfast',
                'navs_target'     => '_self',
            ],
            //侧边栏
            [
                'term_order'      => 1,
                'term_parent'     => 0,
                'term_type'       => 'navs',
                'term_action'     => 'sitebar',
                'term_controll'   => 'user',
                'term_module'     => 'daohang',
                'term_status'     => 'private',
                'term_name'       => '发布网址',
                'term_slug'       => 'publishweb',
                'term_info'       => '用户中心发布网址',
                'navs_url'        => 'daohang/user/web',
                'navs_active'     => 'daohanguserweb',
                'navs_target'     => '_self',
            ],
            [
                'term_order'      => 1,
                'term_parent'     => 0,
                'term_type'       => 'navs',
                'term_action'     => 'sitebar',
                'term_controll'   => 'user',
                'term_module'     => 'daohang',
                'term_status'     => 'private',
                'term_name'       => '发布公众号',
                'term_slug'       => 'publishmp',
                'term_info'       => '用户中心发布公众号',
                'navs_url'        => 'daohang/user/mp',
                'navs_active'     => 'daohangusermp',
                'navs_target'     => '_self',
            ],
            [
                'term_order'      => 1,
                'term_parent'     => 0,
                'term_type'       => 'navs',
                'term_action'     => 'sitebar',
                'term_controll'   => 'user',
                'term_module'     => 'daohang',
                'term_status'     => 'private',
                'term_name'       => '发布小程序',
                'term_slug'       => 'publishmini',
                'term_info'       => '用户中心发布小程序',
                'navs_url'        => 'daohang/user/mini',
                'navs_active'     => 'daohangusermini',
                'navs_target'     => '_self',
            ],
            //底部链接
            [
                'term_order'      => 0,
                'term_parent'     => 0,
                'term_type'       => 'navs',
                'term_action'     => 'links',
                'term_controll'   => 'publish',
                'term_module'     => 'daohang',
                'term_status'     => 'normal',
                'term_name'       => '导航地图',
                'term_slug'       => 'dhditu',
                'term_info'       => '导航系统网站地图模块',
                'navs_url'        => 'daohang/sitemap/index',
                'navs_active'     => 'daohangsitemapindex',
                'navs_target'     => '_self',
            ],
            [
                'term_order'      => 0,
                'term_parent'     => 0,
                'term_type'       => 'navs',
                'term_action'     => 'links',
                'term_controll'   => 'search',
                'term_module'     => 'daohang',
                'term_status'     => 'normal',
                'term_name'       => '导航搜索',
                'term_slug'       => 'dhsearch',
                'term_info'       => '导航系统搜索模块',
                'navs_url'        => 'daohang/search/index',
                'navs_active'     => 'daohangsearchindex',
                'navs_target'     => '_self',
            ],
            [
                'term_order'      => 0,
                'term_parent'     => 0,
                'term_type'       => 'navs',
                'term_action'     => 'links',
                'term_controll'   => 'category',
                'term_module'     => 'daohang',
                'term_status'     => 'normal',
                'term_name'       => '导航分类',
                'term_slug'       => 'dhtypes',
                'term_info'       => '导航系统栏目分类',
                'navs_url'        => 'daohang/category/all',
                'navs_active'     => 'daohangcategoryall',
                'navs_target'     => '_self',
            ],
            [
                'term_order'      => 0,
                'term_parent'     => 0,
                'term_type'       => 'navs',
                'term_action'     => 'links',
                'term_controll'   => 'tag',
                'term_module'     => 'daohang',
                'term_status'     => 'normal',
                'term_name'       => '导航标签',
                'term_slug'       => 'dhtags',
                'term_info'       => '导航系统标签列表',
                'navs_url'        => 'daohang/tag/all',
                'navs_active'     => 'daohangtagall',
                'navs_target'     => '_self',
            ],
            [
                'term_order'      => 0,
                'term_parent'     => 0,
                'term_type'       => 'navs',
                'term_action'     => 'links',
                'term_controll'   => 'publish',
                'term_module'     => 'daohang',
                'term_status'     => 'normal',
                'term_name'       => '免费收录',
                'term_slug'       => 'dhpublish',
                'term_info'       => '免费发布网站、公众号、小程序',
                'navs_url'        => 'daohang/publish/index',
                'navs_active'     => 'daohangpublishindex',
                'navs_target'     => '_self',
            ],
            [
                'term_order'      => 0,
                'term_parent'     => 0,
                'term_type'       => 'navs',
                'term_action'     => 'links',
                'term_controll'   => 'publish',
                'term_module'     => 'daohang',
                'term_status'     => 'normal',
                'term_name'       => '快审服务',
                'term_slug'       => 'dhpublish',
                'term_info'       => '快速发布网站、公众号、小程序',
                'navs_url'        => 'daohang/publish/fast',
                'navs_active'     => 'daohangpublishfast',
                'navs_target'     => '_self',
            ],
        ]);
    }
    
    //批量添加分类
    public function insertCategory()
    {
        config('common.validate_name', false);
        
        config('common.validate_scene', false);
        
        config('common.where_slug_unique', ['term_module'=>['eq','daohang'],'term_type'=>['eq','category']]);
        
        //添加网站一级分类
        $this->insertTerm('category', 'web', 'channel', ['网站大全'], '', 'web-channel');
        
        //添加网站二级分类
        $this->insertTerm('category', 'web', 'index', ['生活服务','综合其他','休闲娱乐','教育文化','行业企业','网络科技','政府组织','购物网站','新闻媒体','交通旅游','医疗健康','体育健身'], '网站大全');
        
        //添加公众号一级分类
        $this->insertTerm('category', 'mp', 'channel', ['公众号大全'], '', 'mp-channel');
        
        //添加公众号二级分类
        $this->insertTerm('category', 'mp', 'index', ['资讯阅读','生活购物','影音娱乐','社区交友','医疗健康','体育教育','行业企业','名人明星','金融理财','综合其他'], '公众号大全');
        
        //添加小程序一级分类
        $this->insertTerm('category', 'mini', 'channel', ['小程序大全'], '', 'mini-channel');
        
        //添加小程序二级分类
        $this->insertTerm('category', 'mini', 'index', ['游戏','影音','搞笑','运动','出行','购物','金融','图像','工具','阅读','教育','生活','健康','办公','社交','资讯','其他'], '小程序大全');
        
        //添加单页一级分类
        $this->insertTerm('category', 'page', 'channel', ['排行榜']);
        
        //添加单页二级分类
        $this->insertTerm('category', 'page', 'index', ['最新网站'], '排行榜', 'web-zuixin');
        $this->insertTerm('category', 'page', 'index', ['热门网站'], '排行榜', 'web-remen');
        $this->insertTerm('category', 'page', 'index', ['最新公众号'], '排行榜', 'mp-zuixin');
        $this->insertTerm('category', 'page', 'index', ['热门公众号'], '排行榜', 'mp-remen');
        $this->insertTerm('category', 'page', 'index', ['最新小程序'], '排行榜', 'mini-zuixin');
        $this->insertTerm('category', 'page', 'index', ['热门小程序'], '排行榜', 'mini-remen');
    }
    
    /**
     * 批量添加标签或分类
     * @version 1.2.0 首次引入
     * @param string $type 必需;队列方式(category|tag);默认：category
     * @param array $post 必需;数组格式,支持的字段列表请参考手册;默认：空
     * @param string $parentName 可选;父级名称,默认：空
     * @return mixed $mixed 成功时返回obj,失败时null
     */
    private function insertTerm($type='category', $controll='web', $action='index', $post=[], $parentName='', $tplPath='')
    {
        $default = [
            'term_name'       => '网站大全',
            'term_slug'       => 'wangzhandaquan',
            'term_module'     => 'daohang',
            'term_controll'   => $controll,
            'term_action'     => $action,
            'term_type'       => $type,
            'term_status'     => 'normal',
            'term_order'      => 2,
            'term_info'       => '收录优秀网站',
            'term_parent'     => 0,
            'term_navbar'     => 'yes',
            'term_tpl'        => $tplPath,
        ];
        $list = [];
        if($parentName){
            foreach($post as $key=>$value){
                array_push($list, DcArrayArgs([
                    'term_name' => $value,
                    'term_slug' => DcPinYin($value),
                    'term_info' => '收录与'.$value.'相关的网站、公众号、小程序',
                    'term_parent' => DcTermNameToId($parentName),
                ], $default));
            }
        }else{
            foreach($post as $key=>$value){
                array_push($list, DcArrayArgs([
                    'term_name' => $value,
                    'term_slug' => DcPinYin($value),
                    'term_info' => '收录与'.$value.'相关的网站、公众号、小程序',
                ], $default));
            }
        }
        return \daicuo\Term::save_all($list);
    }
}